//
//  JMUpQuestionViewController.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/9/7.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMUpQuestionViewController.h"
#import "JMDropCicleView.h"
#import "JMMuenView.h"
#import "JMAppNetworkClient.h"

@interface JMUpQuestionViewController ()<UITextViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{

    NSString * grade;
    NSString * subjectString;
    UIImage * currImage;

}

@property (nonatomic, strong) JMMuenView *muenView;
@property (nonatomic, strong) JMDropCicleView *dropView;

@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) UIImageView *imageViwe;

@property (nonatomic, strong) UIButton *addButton;

@property (nonatomic, strong) UIButton *sendBtn;

@end

@implementation JMUpQuestionViewController


- (JMDropCicleView *)dropView
{
    if (!_dropView) {
        _dropView = [[JMDropCicleView alloc]initWithFrame:CGRectMake(0, self.muenView.frameBottom, UI_SCREEN_WITH, UI_SCREEN_HEIGHT) contentHeight:290];
    }
    return _dropView;
}

- (JMMuenView *)muenView{
    if (!_muenView) {
        JMMuenViewItem *item = [[JMMuenViewItem alloc]initWithImage:[UIImage imageNamed:@"projectImage.bundle/seleted_image_cicle"] title:@"选择年级、学科" target:self action:@selector(muenItemClick:)];
        _muenView =[[JMMuenView alloc]initWithFrame:CGRectMake(-0.5, 64, UI_SCREEN_WITH + 1, 40) withItem:item];
        _muenView.backgroundColor = [UIColor whiteColor];
        _muenView.layer.masksToBounds =YES;
        _muenView.layer.borderColor = [UIColorFromRGB(0xc4c4c4) CGColor];
        _muenView.layer.borderWidth = 0.5;
    }
    return _muenView;
}
- (UIImageView *)imageViwe
{
    if (!_imageViwe) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.layer.masksToBounds =YES;
        imageView.backgroundColor = [UIColor whiteColor];
        imageView.userInteractionEnabled =YES;
        _imageViwe = imageView;
    }
    return _imageViwe;
}

- (UIButton *)addButton
{
    if (!_addButton) {
        UIButton *button = [[UIButton alloc]init];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(addButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageNamed:@"projectImage.bundle/image_seled"] forState:UIControlStateNormal];
        _addButton = button;
    }
    return _addButton;
}


- (UIButton *)sendBtn{
    if (!_sendBtn) {
        UIButton *button = [[UIButton alloc]init];
        [button setTitle:@"发送" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setBackgroundColor:UIColorFromRGB(0xeb6100)];
        [button addTarget:self action:@selector(sendButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _sendBtn = button;
    }
    return _sendBtn;
}

- (UITextView *)textView
{
    if (!_textView) {
        _textView = [[UITextView alloc]init];
        _textView.delegate =self;
    }
    return _textView;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationItem.title = @"学习圈";
    self.backBtn.hidden =YES;
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageViwe.frame =CGRectMake(5, self.muenView.frameBottom + 10, UI_SCREEN_WITH- 10, UI_SCREEN_WITH *0.4525);
    self.imageViwe.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.imageViwe];
    
    
    self.addButton.frame = CGRectMake(self.imageViwe.center.x - 30, self.imageViwe.frameHeight/2.0 - 30, 60, 60);
    self.addButton.backgroundColor = [UIColor clearColor];
    [self.imageViwe addSubview:self.addButton];
    
    self.textView.frame =CGRectMake(5, self.imageViwe.frameBottom + 10, UI_SCREEN_WITH - 10, UI_SCREEN_WITH *0.234375);
    [self.view addSubview:self.textView];

    self.sendBtn.frame = CGRectMake((UI_SCREEN_WITH - UI_SCREEN_WITH *0.625)/2.0, self.textView.frameBottom + 15, UI_SCREEN_WITH *0.625, UI_SCREEN_WITH *0.109375);
    [self.view addSubview:self.sendBtn];

    
    [self.view addSubview:self.dropView];
    [self.view addSubview:self.muenView];
    [self showMuenView];
    
    UITapGestureRecognizer *pan = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.view addGestureRecognizer:pan];
}
#pragma jian  pan xiao shi
-(void)handlePan:(UITapGestureRecognizer *)tap{
    [self.textView resignFirstResponder];
}
-(void)muenItemClick:(JMMuenView *)sender{
    if(sender.rotate){
        [self.dropView hide];
    }else{
        [self showMuenView];
    }
}
-(void)showMuenView{
    [self.dropView showDataPointToView:self.view SelectedItem:^(NSString *title,NSString *title2,JMDropCicleView *dropdownView) {
        grade = title;
        subjectString =title2;
        self.muenView.titleLabel.text = [NSString stringWithFormat:@"%@、%@",grade,subjectString];
        [self.muenView rotating];
    } diss:^(JMDropCicleView *dropdownView) {
    
    }];
    
}
-(void)addButtonClick:(UIButton *)sender{
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypeCamera;
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }

}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    UIImage  * image = [info objectForKey:UIImagePickerControllerEditedImage];
    currImage = image;
    self.addButton.hidden = YES;
    self.imageViwe.image = image;
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

-(void)sendButtonClick:(UIButton *)sender{
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:0];
    [dict setObject:cf.deviceString forKeyedSubscript:@"device"];
    [dict setObject:cf.signString forKeyedSubscript:@"sign"];
    [dict setObject:cf.projectID forKeyedSubscript:@"appid"];
    
    if([grade isEqualToString:@""]){
        [JMTools showAlertView:self.view message:@"你没有选择年级"];
        return;
    }
    if([subjectString isEqualToString:@""]){
        [JMTools showAlertView:self.view message:@"你没有选择学科"];
        return;
    }
    if(!currImage){
        [JMTools showAlertView:self.view message:@"你没有上传问题图片"];
        return;
    }
    if(currImage){
        if(cf.user){
            [dict setObject:cf.user.userId forKeyedSubscript:@"user"];
            [dict setObject:cf.user.token forKeyedSubscript:@"token"];
            [dict setObject:grade forKeyedSubscript:@"grade"];
            [dict setObject:subjectString forKeyedSubscript:@"subject"];
            [dict setObject:self.textView.text forKeyedSubscript:@"detail"];
            
            [JMTools postImageRequestAFNetWorkingDict:dict imageArray:@[currImage] type:@"2" block:^(id data, NSError *error) {
                NSLog(@"%@",data);
            }];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
